<?php
$group_project = $vars ['group_project'];
$semester = $vars ['semester'];
$academic_year = $vars ['academic_year'];

$group = get_entity ( $vars ['group_guid'] );
$listproj_url = elgg_get_site_url () . "submana/listpro/" . $vars ['group_guid'];
elgg_push_breadcrumb ( "Thống kê: " . $group->name, $listproj_url );
elgg_push_breadcrumb ( "Lớp học $group_project" );

$metadata_options = array (
		array (
				'name' => 'group_project',
				'value' => $group_project 
		),
		array (
				'name' => 'semester',
				'value' => $semester 
		),
		array (
				'name' => 'academic_year',
				'value' => $academic_year 
		) 
);

$options = array (
		'type' => "object",
		'subtype' => 'project',
		'deadline_edited' => $ar_deadline_editted [$index],
		'container_guid' => $vars ['group_guid'],
		'hidden_register' => $hidden_register,
		'metadata_name_value_pairs' => $metadata_options 
);

$projectlist = elgg_get_entities_from_metadata ( $options );

$title = elgg_echo ( "Thống kê lớp học $group_project, $semester, $academic_year" );
$content = elgg_view_title ( $title );

foreach ( $projectlist as $project ) {
	
	if ($project->wait_project) {
		continue;
	}
	
	$project_name = elgg_view ( "output/url", array (
			'href' => $project->group_url,
			'text' => $project->title 
	) );
	$content .= "<br><hr>";
	$content .= "<div>";
	$content .= "<b>";
	$content .= $project_name;
	$content .= "</b><br><br>";
	
	
	if (! $project->approved) {
		$content .= "<div style='text-align:center; color:red;'>Sinh viên chưa đăng kí đề tài này</div>";
		continue;
	}
	
	
	// ----------------------
	// add list group member
	$html_students = "<div class = 'margin_left_15'>";
	$html_students .= "<table class='' style='width:100%;'>";
	$html_students .= "<tr>
						<th style = 'background: rgb(207, 202, 202);'>Thành viên</th>
						<th style = 'background: rgb(207, 202, 202);'>Mã số sinh viên</th>
						<th style = 'background: rgb(207, 202, 202);'>Commit SVN</th>
						<th style = 'background: rgb(207, 202, 202);'>Files upload</th>
						<th style = 'background: rgb(207, 202, 202);'>Chủ đề thảo luận</th>
			</tr>";
	$register_owner = true;
	
	$students = $project->students;
	
	if (is_array ( $students )) {
		foreach ( $students as $student_guid ) {
			$student = get_user_by_username ( $student_guid );
			// $student = get_entity ( $student_guid );
			
			if ($student) {
				if ($register_owner == true) {
					$register_owner = false;
					$red = "color:blue;";
				} else {
					$red = "";
				}
				$count = 0;
				if (isset ( $student->svn_commit_count )) {
					try {
						$array = json_decode ( $student->svn_commit_count, true );
						if (! empty ( $array [$project->group_guid] )) {
							// var_dump($array[$project->group_guid]);
							$count = $array [$project->group_guid];
						}
					} catch ( Exception $e ) {
					}
				}
				
				$html_students .= "<tr><td><a href ='{$student->getURL()}'><span>{$student->name}</span></a>
				</td><td style='text-align:center;'><span style = '$red'>$student->username</span></td>
				<td style='text-align:center;'>$count</td>";
				
				$files = elgg_get_entities ( array (
						'type' => "object",
						'subtype' => "file",
						'owner_guid' => $student->getGUID (),
						'container_guid' => $project->group_guid 
				) );
				$count = 0;
				$count = count ( $files );
				$html_students .= "<td style='text-align:center;'>$count</td>";
				
				$discussion = elgg_get_entities ( array (
						'type' => "object",
						'subtype' => "groupforumtopic",
						'owner_guid' => $student->getGUID (),
						'container_guid' => $project->group_guid 
				) );
				$count = 0;
				$count = count ( $discussion );
				$html_students .= "<td style='text-align:center;'>$count</td></tr>";
			}
		}
	} else {
		$student = get_user_by_username ( $students );
		// $student = get_entity ( $student_guid );
		
		if ($student) {
			if ($register_owner == true) {
				$register_owner = false;
				$red = "color:blue;";
			} else {
				$red = "";
			}
			
			$html_students .= "<tr><td><a href ='{$student->getURL()}'><span>{$student->name}</span></a></td><td><span style = '$red'>$student->username</span></td></tr>";
		}
	}
	$html_students .= "</table>";
	$html_students .= "</div>";
	
	// -----------------------
	$content .= $html_students;

	$url_reivew = elgg_get_site_url () . "groups/review/{$project->group_guid}";
	$url_reivew = elgg_add_action_tokens_to_url ( $url_reivew );
	$review = elgg_view ( 'output/url', array (
			'text' => "Nhận xét",
			'href' => $url_reivew,
			'class' => "elgg-button"
	) );
	$content .= $review . "<br>";
	$content .= "</div>";
}

$sidebar = "";
$params = array (
		'content' => $content,
		'sidebar' => $sidebar 
);

$body = elgg_view_layout ( 'one_sidebar', $params );
// start building the main column of the page

echo elgg_view_page ( $title, $body );